/*
 * The National Parks Database Project
 * Group: Tyler Simrell, Vladmir Dudnic, Michael Jagielo
 * HikerPanel.java
 */

package panels;

import gui.FieldParser;
import gui.MainFrame;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This class is the JPanel that represents the GUI that the hiker uses to search for trails.
 * @author Tyler Simrell
 * @version 5/24/13
 */
@SuppressWarnings("serial")
public class HikerPanel extends JPanel implements ActionListener
{
	public final static String QUERY_DIFFICULTY_LEVELS[] = {"Easy", "Medium", "Hard", "Don't Care"};
	public final static String QUERY_TERRAIN_TYPES[] = {"Forest", "Meadow", "Don't Care"};
	
	CardLayout cards;
	JPanel cardPanel; 
	ResultsPanel resultPanel;
	JTextField trailNameField;
	JTextField parkNameField;
	JTextField vistaPointField;
	JTextField slopeField;
	JTextField lengthField;
	JComboBox<String> terrainComboBox;
	JComboBox<String> difficultyComboBox;
	
	/**
	 * This constructor takes in a reference to the card layout and the JPanel that houses the card layout.
	 * @param passedInCards The card layout for all of the panels.
	 * @param passedInPanel The JPanel that the card layout is on top of.
	 */
	public HikerPanel(CardLayout passedInCards, JPanel passedInPanel, ResultsPanel passedInRP)
	{
		super();
		cards = passedInCards;
		cardPanel = passedInPanel;
		resultPanel = passedInRP;
		setUp();
	}
	
	/**
	 * This method does much of the heavy lifting in regards to setting up the GUI.
	 */
	private void setUp()
	{
		cards.addLayoutComponent(this, MainFrame.HIKING_PANEL);
		this.setLayout(null);
		
		JLabel panelTitleLabel = new JLabel("Welcome Hiker!");
		panelTitleLabel.setFont(new Font("Tempus Sans ITC", Font.PLAIN, 18));
		panelTitleLabel.setBounds(348, 11, 137, 30);
		this.add(panelTitleLabel);
		
		JLabel instructions = new JLabel("Please Enter The Applicable Information To Find A Trail");
		instructions.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		instructions.setBounds(199, 52, 423, 30);
		this.add(instructions);
		
		JLabel trailNameLabel = new JLabel("Name of the Trail");
		trailNameLabel.setBounds(51, 94, 155, 16);
		this.add(trailNameLabel);
		
		trailNameField = new JTextField();
		trailNameField.setToolTipText("The Name of the Trail");
		trailNameField.setBounds(199, 94, 413, 20);
		this.add(trailNameField);
		trailNameField.setColumns(10);
		
		JLabel parkNameLabel = new JLabel("Park Name");
		parkNameLabel.setBounds(51, 122, 101, 16);
		this.add(parkNameLabel);
		
		parkNameField = new JTextField();
		parkNameField.setToolTipText("The name of the park that the trail is in. If the trail spans two parks, it " +
				"will be the name of the park it starts in. ");
		parkNameField.setBounds(199, 120, 413, 20);
		this.add(parkNameField);
		parkNameField.setColumns(10);
		
		JLabel vistaPointLabel = new JLabel("Vista Point");
		vistaPointLabel.setBounds(51, 150, 68, 16);
		this.add(vistaPointLabel);
		
		vistaPointField = new JTextField();
		vistaPointField.setToolTipText("The name of the major vista point on the trail");
		vistaPointField.setBounds(199, 148, 413, 20);
		this.add(vistaPointField);
		vistaPointField.setColumns(10);
		
		JLabel slopeLabel = new JLabel("Slope");
		slopeLabel.setBounds(51, 178, 55, 16);
		this.add(slopeLabel);
		
		slopeField = new JTextField();
		slopeField.setToolTipText("The slope of the trail (the difference in height at the beginning and ending of the trail). Measured in feet.");
		slopeField.setBounds(199, 176, 68, 20);
		this.add(slopeField);
		slopeField.setColumns(10);
		
		JLabel terrainLabel = new JLabel("Terrain");
		terrainLabel.setBounds(51, 234, 55, 16);
		this.add(terrainLabel);
		
		JLabel lengthLabel = new JLabel("Length");
		lengthLabel.setBounds(51, 206, 55, 16);
		this.add(lengthLabel);
		
		lengthField = new JTextField();
		lengthField.setToolTipText("The total length of the trail. Measured in feet.");
		lengthField.setBounds(199, 204, 68, 20);
		this.add(lengthField);
		lengthField.setColumns(10);
		
		terrainComboBox = new JComboBox<String>();
		terrainComboBox.setToolTipText("The type of terrain that the trail has.");
		terrainComboBox.setBounds(199, 234, 81, 20);
		terrainComboBox.setModel(new DefaultComboBoxModel<String>(QUERY_TERRAIN_TYPES));
		this.add(terrainComboBox);
		
		JLabel difficultyLabel = new JLabel("Difficulty");
		difficultyLabel.setBounds(51, 262, 55, 16);
		this.add(difficultyLabel);
		
		difficultyComboBox = new JComboBox<String>();
		difficultyComboBox.setModel(new DefaultComboBoxModel<String>(QUERY_DIFFICULTY_LEVELS));
		difficultyComboBox.setToolTipText("The overall difficulty of the trail, which is derived from the length, slope, and terrain.");
		difficultyComboBox.setBounds(199, 260, 81, 20);
		this.add(difficultyComboBox);
		
		JButton searchButton = new JButton("Search");
		searchButton.setBounds(352, 395, 98, 26);
		searchButton.addActionListener(this);
		this.add(searchButton);
		
		this.add(new LowerPanel(cards, cardPanel, true));
	}
	
	/**
	 * This method services the action for when the search button has been pressed (or clicked). 
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(trailNameField.getText().isEmpty()&&
		   parkNameField.getText().isEmpty()&&
		   vistaPointField.getText().isEmpty()&&
		   slopeField.getText().isEmpty()&&
		   lengthField.getText().isEmpty()&& 
		   terrainComboBox.getSelectedItem().toString().equals("Don't Care") &&
		   difficultyComboBox.getSelectedItem().toString().equals("Don't Care"))
		{
			JOptionPane.showMessageDialog(null, "You must enter at least one piece of data to search by", 
					"Lack of Information error", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			String difficultyString;
			String terrainString;
			if(difficultyComboBox.getSelectedItem().toString().equals("Don't Care"))
			{
				difficultyString = "";
			}
			else
			{
				difficultyString = difficultyComboBox.getSelectedItem().toString();
			}
			
			if(terrainComboBox.getSelectedItem().toString().equals("Don't Care"))
			{
				terrainString = "";
			}
			else
			{
				terrainString = terrainComboBox.getSelectedItem().toString();
			}
			
			resultPanel.createResultsTable(FieldParser.parseFieldsResults(trailNameField.getText(), 
					parkNameField.getText(), vistaPointField.getText(), slopeField.getText(), 
					lengthField.getText(), terrainString, difficultyString));
			resultPanel.revalidate();
			cards.show(cardPanel, MainFrame.RESULTS_PANEL);	
		}
	}
}
